﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ConsleServer
{

    public struct Rect
    {
        public int Left; //最左坐标
        public int Top; //最上坐标
        public int Right; //最右坐标
        public int Bottom; //最下坐标

        public void Union(Rect other)
        {
            if (other.Left >= other.Right || other.Top >= other.Bottom)
                return;
            Left = Left < other.Left ? Left : other.Left;
            Top = Top < other.Top ? Top : other.Top;
            Right = Right > other.Right ? Right : other.Right;
            Bottom = Bottom > other.Bottom ? Bottom : other.Bottom;
        }
        public Rectangle ToRectangle()
        {
            return new Rectangle(Left, Top, Right - Left, Bottom - Top);
        }
    }
    class Program
    {
        /// <summary>
        /// 通过程序标题查找句柄
        /// </summary>
        /// <param name="lpClassName">窗口类型</param>
        /// <param name="lpWindowName">窗口名称</param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool GetClientRect(IntPtr hWnd, ref Rect lpRect);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool GetWindowRect(IntPtr hWnd, ref Rect lpRect);

        private const int SM_CYCAPTION = 4;
        private const int SM_CXBORDER = 5;
        private const int SM_CXDLGFRAME = 7;
        [DllImport("user32.dll")]
        private static extern int GetSystemMetrics(int nIndex);
        static void Main(string[] args)
        {
            while (true)
            {
                //TestRefrence();
                //ServerManager tManager = new ServerManager();
                //tManager.Regist();
                var ptr = FindWindow(null, "TeamViewer");

                Rect tRect = new Rect();
                GetClientRect(ptr, ref tRect);
                Console.WriteLine(tRect.ToRectangle());

                Rect tRect2 = new Rect();
                GetWindowRect(ptr, ref tRect2);
                Console.WriteLine(tRect2.ToRectangle());


                Console.WriteLine(GetSystemMetrics(SM_CXDLGFRAME));
                Console.WriteLine(GetSystemMetrics(SM_CYCAPTION));
                Console.ReadLine();
            }


            Console.WriteLine(GetSystemMetrics(SM_CXDLGFRAME));
            Console.WriteLine(GetSystemMetrics(SM_CXBORDER));

            Console.Read();
        }

        private static void TestRefrence()
        {
            _class = new TestClass();
            _class.str = "2221";
            var temp = GettestClass();
            Console.WriteLine(temp .str);
        }

        private static TestClass _class;
        private static TestClass GettestClass()
        {
            var temp = _class;
            _class = null;
            return temp;
        }
    }

    class  TestClass
    {
        public string str = "1111";
    }
}
